package jmine.tec.scheduling;

import jmine.tec.scheduling.condition.Condition;
import jmine.tec.scheduling.domain.ScheduledTask;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import bancosys.tec.persist.controller.AbstractPersistenceEnabledController;

/**
 * Controller for the scheduling component
 * 
 * @author lundberg
 */
public class SchedulingController extends AbstractPersistenceEnabledController
		implements BeanFactoryAware {

	private BeanFactory beanFactory;

	/**
	 * Obtains the task condition
	 * 
	 * @param task
	 *            task
	 * @return condition
	 */
	public Condition findCondition(ScheduledTask task) {
		return this.getBean(task.getCondition(), Condition.class);
	}

	/**
	 * Obtains the task action
	 * 
	 * @param task
	 *            task
	 * @return action
	 */
	public Action findAction(ScheduledTask task) {
		return this.getBean(task.getAction(), Action.class);
	}

	/**
	 * Finds a bean from the bean factory with the given name and required type
	 * 
	 * @param <B>
	 *            bean type
	 * @param name
	 *            name
	 * @param requiredType
	 *            requiredType
	 * @return bean
	 */
	private <B> B getBean(String name, Class<B> requiredType) {
		return requiredType.cast(this.beanFactory.getBean(name, requiredType));
	}

	/**
	 * {@inheritDoc}
	 */
	public void setBeanFactory(BeanFactory beanFactory) {
		this.beanFactory = beanFactory;
	}
}
